import 'package:aio/aio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/config/app_data_cache.dart';
import 'package:app/src/manager/ywx_manager.dart';
import 'package:app/src/ui/app_ui.dart';
import 'package:app/src/http/bean/ywx_limit.dart';
import 'package:app/src/ui/dialog/dailog.dart';
import 'package:app/src/ui/route/helper/route_helper.dart';
import 'package:app/src/utils/image_path_utils.dart';

import '../../../../http/bean/worksdo/material_requisition_bean.dart';
import '../../../../http/bean/wroks/dispatchingaudit_bean.dart';
import '../../../../http/model/material_requisition_model.dart';
import '../../../../http/model/review_model.dart';
import '../../../../http/model/user_model.dart';

///背景色
Color backgroundColor() {
  return AppColors.backgroundColorWithWhite;
}

///名称通用搜索
class SearchNameRoute extends StatefulWidget {
  @override
  _State createState() => _State();
  ResponseCallbackWithT responseCallbackWithT;
  List<RollerBean>? datas;

  SearchNameRoute(this.datas, this.responseCallbackWithT);
}

class _State extends State<SearchNameRoute> {
  _Presenter presenter = _Presenter();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    px.init(context);
    var scaffold = Scaffold(
      backgroundColor: backgroundColor(),
      appBar: AppBar(
        //fixme 搜索框位置
        backgroundColor: backgroundColor(),
        elevation: 0,
        leading: AppUi.getBack(context, () {
          return false;
        }),
        leadingWidth: 33,
        titleSpacing: 0,
        title: _search(context),
      ),
      body: Material(color: backgroundColor(), child: _body(context)),
    );
    var willPopScope = WillPopScope(
      child: scaffold,
      onWillPop: () async {
        return true; //true 不屏蔽返回键
      },
    );
    return Material(child: willPopScope, color: backgroundColor());
  }

  ///搜索框
  Widget _search(BuildContext context) {
    Widget widget = StatefulBuilder(
      builder: (BuildContext context, StateSetter setState) {
        presenter?.setStateSearch = setState;
        var textField = TextField(
          textAlign: TextAlign.start,
          textAlignVertical: TextAlignVertical.center,
          onChanged: (value) {
            //文本发生变化
            setState(() {
              presenter?.searContent = value;
            });
            presenter?.setStateBody?.call(() {});
          },
          controller:
              TextUtils.setTextEditingController(text: presenter?.searContent),
          autofocus: true,
          decoration: InputDecoration(
              //尾部删除图标
              suffixIcon: (presenter.searContent!.length > 0)
                  ? Container(
                      width: 40,
                      height: 40,
                      child: InkWell(
                        child: Center(
                          child: Icon(
                            Icons.clear,
                            color: Colors.grey,
                            size: 16,
                          ),
                        ),
                        onTap: () {
                          //文本清除
                          setState(() {
                            presenter.searContent = "";
                          });
                          presenter?.setStateBody?.call(() {});
                        },
                      ),
                    )
                  : null,
              prefixIcon: Icon(Icons.search, color: Colors.black),
              hintText: AppStrings.of(context).search,
              contentPadding: EdgeInsets.symmetric(horizontal: 8, vertical: 8),
              focusedBorder: OutlineInputBorder(
                  borderSide: BorderSide(
                      color: Colors.black, width: 1, style: BorderStyle.solid),
                  borderRadius: BorderRadius.all(Radius.circular(30.0))),
              border: OutlineInputBorder(
                  borderSide: BorderSide(
                      color: Colors.grey, width: 1, style: BorderStyle.solid),
                  borderRadius: BorderRadius.all(Radius.circular(30.0)))),
        );
        return SizedBox(
          child: textField,
          height: 35,
        );
      },
    );
    return Padding(
      padding: EdgeInsets.only(right: 16, top: 0, bottom: 0),
      child: widget,
    );
  }

  //主体布局
  Widget _body(BuildContext context) {
    return StatefulBuilder(
      builder: (BuildContext context, StateSetter setState) {
        presenter?.setStateBody = setState;
        List<RollerBean>? allItems = widget?.datas;
        if (allItems == null) {
          return AppUi.getEmptyView();
        }
        List<RollerBean> searchItems = [];
        if (StringUtils.isNotEmpty(presenter.searContent)) {
          allItems?.forEach((element) {
            if (element.value?.contains(presenter.searContent) ?? false) {
              searchItems.add(element);
            }
          });
        } else if (allItems != null) {
          searchItems = allItems; //没有搜索条件，就显示全部。
        }
        return ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            var item = searchItems[index];
            return Material(
              color: backgroundColor(),
              child: InkWell(
                onTap: () {
                  AppRouteHelper.pop(context); //fixme 先关闭页面。防止页面无法正常关闭。
                  ///搜索条件选中
                  widget.responseCallbackWithT?.call(item);
                },
                child: Container(
                  height: 55,
                  child: ListTile(
                    title: Text(
                      item.value!,
                      style: AppStyle.textStyleWithItem(),
                    ),
                    // leading: Image.asset(
                    //   item.ImagePath!,
                    //   width: 40,
                    //   height: 40,
                    // ),
                  ),
                ),
              ),
            );
          },
          itemCount: searchItems.length,
        );
      },
    );
  }
}

class _Presenter {
  ///body视图状态
  StateSetter? setStateBody;

  ///搜索框视图状态
  StateSetter? setStateSearch;

  //文本搜索内容
  String searContent = "";
}
